package com.iplant.wms.controller.brm;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.BRMService;
import com.iplant.wms.service.po.brm.BRMDistributionProp;
import com.iplant.wms.service.po.brm.BRMDistributionRange;
import com.iplant.wms.service.po.brm.BRMDistributionRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 预分配规则
 */
@RestController
@RequestMapping("/api/Distribution")
public class DistributionController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(DistributionController.class);

    @Autowired
    BRMService wBRMService;

    @GetMapping("/PropAll")
    public Object PropAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            int wRuleID = StringUtils.parseInt(request.getParameter("RuleID"), -1);
            String wRuleCode = StringUtils.parseString(request.getParameter("RuleCode"));
            int wASNTypeID = StringUtils.parseInt(request.getParameter("ASNTypeID"), -1);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<BRMDistributionProp>> wServiceResult = wBRMService.BRM_QueryDistributionPropList(
                    wLoginUser, wRuleID, wRuleCode, wName, wCode, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/PropInfo")
    Object PropInfo(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wRuleID = StringUtils.parseInt(request.getParameter("RuleID"));
            String wRuleCode = StringUtils.parseString(request.getParameter("RuleCode"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<BRMDistributionProp> wServiceResult = wBRMService.BRM_QueryDistributionProp(
                    wLoginUser, wRuleID, wRuleCode, wCode);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(
                        RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServiceResult.Result);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/PropUpdate")
    public Object PropUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMDistributionProp wBRMDistributionProp = CloneTool.Clone(wParam.get("data"), BRMDistributionProp.class);
            wBRMDistributionProp.EditorName = wUserName;
            wBRMDistributionProp.EditorID = wUserID;
            wBRMDistributionProp.EditTime = Calendar.getInstance();
            if (wBRMDistributionProp.ID <= 0) {

                wBRMDistributionProp.CreatorName = wUserName;
                wBRMDistributionProp.CreatorID = wUserID;
                wBRMDistributionProp.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateDistributionProp(
                    wBMSEmployee, wBRMDistributionProp);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMDistributionProp);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/PropActive")
    public Object PropActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<BRMDistributionProp> wBRMDistributionPropList = CloneTool.CloneArray(
                    wParam.get("data"), BRMDistributionProp.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (BRMDistributionProp wBRMDistributionProp : wBRMDistributionPropList) {

                wServiceResult = wBRMService.BRM_ActiveDistributionProp(wBMSEmployee, wBRMDistributionProp, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode())) break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/RuleAll")
    Object RuleAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            int wMethod = StringUtils.parseInt(request.getParameter("Method"), -1);
            int wFirstType = StringUtils.parseInt(request.getParameter("FirstType"), -1);
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<BRMDistributionRule>> wServiceResult = wBRMService.BRM_QueryDistributionRuleList(
                    wLoginUser, wMethod, wFirstType, wName, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/RuleInfo")
    Object RuleInfo(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<BRMDistributionRule> wServiceResult = wBRMService.BRM_QueryDistributionRule(
                    wLoginUser, wID, wCode);
            BRMDistributionRule wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleUpdate")
    Object RuleUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMDistributionRule wBRMDistributionRule = CloneTool.Clone(wParam.get("data"), BRMDistributionRule.class);
            wBRMDistributionRule.EditorName = wUserName;
            wBRMDistributionRule.EditorID = wUserID;
            wBRMDistributionRule.EditTime = Calendar.getInstance();
            if (wBRMDistributionRule.ID <= 0) {

                wBRMDistributionRule.CreatorName = wUserName;
                wBRMDistributionRule.CreatorID = wUserID;
                wBRMDistributionRule.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateDistributionRule(
                    wBMSEmployee, wBRMDistributionRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMDistributionRule);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleDelete")
    Object RuleDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMDistributionRule wBRMDistributionRule = CloneTool.Clone(wParam.get("data"), BRMDistributionRule.class);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteDistributionRule(
                    wBMSEmployee, wBRMDistributionRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleActive")
    Object RuleActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<BRMDistributionRule> wBRMDistributionRuleList = CloneTool.CloneArray(
                    wParam.get("data"), BRMDistributionRule.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (BRMDistributionRule wBRMDistributionRule : wBRMDistributionRuleList) {

                wServiceResult = wBRMService.BRM_ActiveDistributionRule(wBMSEmployee, wBRMDistributionRule, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode())) break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @GetMapping("/RangeAll")
    Object RangeAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wMaterialGroupID = StringUtils.parseInt(request.getParameter("MaterialGroupID"), -1);
            int wMaterialID = StringUtils.parseInt(request.getParameter("MaterialID"), -1);
            String wMaterialLike = StringUtils.parseString(request.getParameter("MaterialLike"));
            String wMaterialCode = StringUtils.parseString(request.getParameter("MaterialCode"));

            String wMaterialGroupFullName = StringUtils.parseString(request.getParameter("MaterialGroupFullName"));
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<BRMDistributionRange>> wServiceResult = wBRMService.BRM_QueryDistributionRangeList(
                    wLoginUser, wMaterialGroupID, wMaterialGroupFullName, wMaterialID, wMaterialCode, wMaterialLike,
                    wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/RangeUpdate")
    Object RangeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMDistributionRange wBRMDistributionRange = CloneTool.Clone(
                    wParam.get("data"), BRMDistributionRange.class);

            wBRMDistributionRange.setUserInfo(wBMSEmployee);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateDistributionRange(
                    wBMSEmployee, wBRMDistributionRange);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMDistributionRange);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/RangeDelete")
    Object RangeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMDistributionRange wBRMDistributionRange = CloneTool.Clone(
                    wParam.get("data"), BRMDistributionRange.class);

            wBRMDistributionRange.setUserInfo(wBMSEmployee);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteDistributionRange(
                    wBMSEmployee, wBRMDistributionRange);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMDistributionRange);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


}
